package com.mju.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mju.dao.ProductDao;
import com.mju.pojo.Product;
import com.mju.service.IProductService;
import com.mju.service.ex.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductDao productDao;

    @Override
    public List<Product> selectHotList() {
        List<Product> products = productDao.selectHotList();
        for (Product product: products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public Product selectById(Integer id) {
        Product product = productDao.selectById(id);
        if(product == null) {
            throw new ProductNotFoundException("商品数据不存在");
        }
        // 将查询结果中的部分属性设置为null
        product.setPriority(null);
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedUser(null);
        product.setModifiedTime(null);
        return product;
    }

    @Override
    public IPage<Product> queryProductByTitle(Integer currentPage, Integer pageSize, String title) {
        IPage page = new Page<>(currentPage, pageSize);
        // 构建查询构建器，根据标题进行模糊查询
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getStatus, 1)
                .like(Product::getTitle, title)
                .orderByDesc(Product::getPriority);
        productDao.selectPage(page, lqw);
        return page;
    }
}
